package leetcode.day_2021_03_8_2021_03_14;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 *
 */
public class LeetCode0049 {

    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<>();
        HashMap<String, List> map = new HashMap<>();
        int len = strs.length;
        String[] strDemo = new String[len];
        sortString(strs, strDemo);
        for (int i=0; i<len; i++) {
            if (!map.containsKey(strDemo[i])) {
                List<String> list = new ArrayList();
                list.add(strs[i]);
                map.put(strDemo[i], list);
            } else {
                map.get(strDemo[i]).add(strs[i]);
            }
        }
        map.entrySet().stream().forEach((entry -> res.add(entry.getValue())));
        return res;
    }

    public static void sortString(String[] str, String[] strDemo) {
        for (int i=0; i<str.length; i++) {
            char[] chars = str[i].toCharArray();
            Arrays.sort(chars);
            strDemo[i] = new String(chars);
        }
    }

    public static void main(String[] args) {

        System.out.println(groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}).toString());
    }
}
